Graphs
# Packages
library(tidyverse)
library(remotes)
library(ggplot2)
library(RColorBrewer) #RColorBrewer::display.brewer.all()
# http://www.stat.columbia.edu/~tzheng/files/Rcolor.pdf
# https://github.com/EmilHvitfeldt/r-color-palettes/blob/master/README.md#comprehensive-list-of-color-palettes-in-r
library(showtext)
library(sysfonts)
font_add_google("Playfair Display", ## name of Google font
"Playfair") ## name that will be used in R
font_add_google("Bangers", "Bangers")
font_add_google("Merriweather", "Merriweather")
font_add_google("Lato", "Lato")
showtext_auto()
library(plyr) #ddply
library(dplyr) #mutate
library(ggdist) #violin
# 3d models penguins graphs: https://gist.github.com/tylermorganwall/6397f160e4e587757199fe819b6cb040The palmerpenguins package contains two datasets with 344 penguins.
There are 3 different species of penguins in this dataset, collected from 3 islands in the Palmer Archipelago, Antarctica. The first dataset (penguins) is the simplified version of the raw data, and the second dataset (penguins_raw) contains all the variables and original names.
# Penguin Dataset
# remotes::install_github("allisonhorst/palmerpenguins")
library(palmerpenguins)
# Getting the 2 datasets from the package to the global environment
penguins <- penguins # ?penguins for more information
penguins_raw <- penguins_raw # ?penguins_raw for more informationPenguin Bills
Bar Graphs
Frequence Absolute (N)
Basic
(penguins %>% # Dataset
ggplot(aes(sex)) + # Coluna de interesse
geom_bar()) # Formato de barrasComplete
(penguins %>% drop_na(sex) %>% # Remove os NAs
ggplot(aes(sex)) + # Coluna de interesse
geom_bar(aes(y = ..count.., fill = factor(..x..)), stat="count", position = 'dodge', width = .7) + # Estética das barras e eixo y com frequência absoluta
geom_text(aes(label = ..count..),stat= "count", vjust = -0.3, hjust = 0.4, size = 3, family="Lato", fontface = "bold") + # texto em cima das barras
theme_bw()+
theme(legend.position = "none", # Formatação dos textos e eixos
plot.title = element_text(family = "Playfair", color="black", size=20, face="bold.italic"),
plot.subtitle = element_text(family = "Merriweather", color="gray24", size=12, face="bold.italic"),
axis.title = element_text(family = "Lato", color="black", size = 12, face="bold"),
axis.text = element_text(family = "Lato", color="black", size = 10, face="bold"))+
labs(title = "Bar Graph", # TÃtulo, subtÃtulo e tÃtulos dos eixos
subtitle = "Sex",
x = "Sex",
y = "Absolute Frequence (N)") +
scale_fill_brewer(palette="Set1") + # Cor das barras
scale_color_brewer(palette="Set1")) + # Cor do contorno das barras
scale_x_discrete(labels= c("Female", "Male")) # Renomeando texto no eixo xSeparated by groups
(penguins %>% drop_na(sex) %>% # Remove os NAs
ggplot(aes(sex)) + # Coluna de interesse
geom_bar(aes(y = ..count.., fill = factor(..x..)), stat="count", position = 'dodge', width = .7) + # Estética das barras e eixo y com frequência absoluta
geom_text(aes(label = ..count..),stat= "count", vjust = -0.3, hjust = 0.4, size = 3, family="Lato", fontface = "bold") + # texto em cima das barras
theme_bw()+
theme(legend.position = "none", # Formatação dos textos e eixos
strip.text = element_text(size=12, color="white", family="Bangers"),
strip.background = element_rect (fill="gray24"),
plot.title = element_text(family = "Playfair", color="black", size=20, face="bold.italic"),
plot.subtitle = element_text(family = "Merriweather", color="gray24", size=12, face="bold.italic"),
axis.title = element_text(family = "Lato", color="black", size = 12, face="bold"),
axis.text = element_text(family = "Lato", color="black", size = 10, face="bold"))+
labs(title = "Bar Graph", # TÃtulo, subtÃtulo e tÃtulos dos eixos
subtitle = "Sex by Species",
x = "Sex",
y = "Absolute Frequence (N)") +
scale_fill_brewer(palette="Set1") + # Cor das barras
scale_color_brewer(palette="Set1") + # Cor do contorno das barras
scale_x_discrete(labels= c("Female", "Male"))+ # Renomeando texto no eixo x
facet_wrap(~species, nrow = 1, labeller = labeller (Group = labels))) # Segunda variável separando o gráfico em mais gráficosRelative Frequence (%)
Basic
(penguins %>% drop_na(sex) %>%
ggplot(aes(sex)) +
geom_bar(aes(y = (..count..)/sum(..count..))))Complete
(penguins %>% drop_na(sex) %>% # Remove os NAs
ggplot(aes(sex)) + # Coluna de interesse
geom_bar(aes(y = (..count..)/sum(..count..), fill = factor(..x..)), stat="count", position = 'dodge', width = .7) + # Estética das barras e eixo y com frequência relativa
geom_text(aes(label = scales::percent(round((..count..)/sum(..count..), 3)), y = (..count..)/sum(..count..)), stat= "count", vjust = -0.3, hjust = 0.4, size = 3, family="Lato", fontface = "bold") + # texto em cima das barras
scale_y_continuous(labels=scales::percent_format(accuracy = 1), limits = c(0,1)) + # Transformar o eixo y em porcentagem e criar limites
theme_bw()+
theme(legend.position = "none", # Formatação dos textos e eixos
plot.title = element_text(family = "Playfair", color="black", size=20, face="bold.italic"),
plot.subtitle = element_text(family = "Merriweather", color="gray24", size=12, face="bold.italic"),
axis.title = element_text(family = "Lato", color="black", size = 12, face="bold"),
axis.text = element_text(family = "Lato", color="black", size = 10, face="bold"))+
labs(title = "Bar Graph", # TÃtulo, subtÃtulo e tÃtulos dos eixos
subtitle = "Sex",
x = "Sex",
y = "Relative Frequence (%)") +
scale_fill_brewer(palette="Set1") + # Cor das barras
scale_color_brewer(palette="Set1")) + # Cor do contorno das barras
scale_x_discrete(labels= c("Female", "Male")) # Renomeando texto no eixo xSeparated by groups
(penguins %>% drop_na(sex) %>% # Remove os NAs
ggplot(aes(sex, group = species)) + # Coluna de interesse
geom_bar(aes(y = ..prop.., fill = factor(..x..)), stat="count", position = 'dodge', width = .7) + # Estética das barras e eixo y com frequência relativa
geom_text(aes(label = scales::percent(round(..prop.., 3)), y = ..prop..), stat= "count", vjust = -0.3, hjust = 0.4, size = 3, family="Lato", fontface = "bold") + # texto em cima das barras
scale_y_continuous(labels=scales::percent_format(accuracy = 1), limits = c(0,1)) + # Transformar o eixo y em porcentagem e criar limites
theme_bw()+
theme(legend.position = "none", # Formatação dos textos e eixos
strip.text = element_text(size=12, color="white", family="Bangers"),
strip.background = element_rect (fill="gray24"),
plot.title = element_text(family = "Playfair", color="black", size=20, face="bold.italic"),
plot.subtitle = element_text(family = "Merriweather", color="gray24", size=12, face="bold.italic"),
axis.title = element_text(family = "Lato", color="black", size = 12, face="bold"),
axis.text = element_text(family = "Lato", color="black", size = 10, face="bold"))+
labs(title = "Bar Graph", # TÃtulo, subtÃtulo e tÃtulos dos eixos
subtitle = "Sex by Species",
x = "Sex",
y = "Relative Frequence (%)") +
scale_fill_brewer(palette="Set1") + # Cor das barras
scale_color_brewer(palette="Set1")) + # Cor do contorno das barras
scale_x_discrete(labels= c("Female", "Male"))+ # Renomeando texto no eixo x
facet_wrap(~species, nrow = 1, labeller = labeller (Group = labels)) # Segunda variável separando o gráfico em mais gráficosHistogram
Basic
(penguins %>% drop_na(flipper_length_mm) %>%
ggplot(aes(x = flipper_length_mm)) +
geom_histogram())Complete
(penguins %>% drop_na(flipper_length_mm) %>% # remover NAs
ggplot(aes(x = flipper_length_mm)) + # variável quantitativa para o eixo x
geom_histogram(alpha=1, position = "identity", color="black", fill="firebrick", bins = 20) + # bins é a quantidade de colunas
theme_bw()+
theme(legend.position = "none", # Formatação dos textos e eixos
strip.text = element_text(size=12, color="white", family="Bangers"),
strip.background = element_rect (fill="gray24"),
plot.title = element_text(family = "Playfair", color="black", size=20, face="bold.italic"),
plot.subtitle = element_text(family = "Merriweather", color="gray24", size=12, face="bold.italic"),
axis.title = element_text(family = "Lato", color="black", size = 12, face="bold"),
axis.text = element_text(family = "Lato", color="black", size = 10, face="bold"))+
labs(title = "Histogram", # TÃtulo, subtÃtulo e tÃtulos dos eixos
subtitle = "Flipper Length",
x = "Flipper Length (mm)",
y = 'Absolute Frequence (N)'))Separated by groups
(penguins %>% drop_na(flipper_length_mm) %>% # remover NAs
ggplot(aes(x = flipper_length_mm, fill=species)) + # variável quantitativa para o eixo x
geom_histogram(alpha=0.4, position = "identity", color="black", bins = 20) + # alpha muda densidade da cor, bins é a quantidade de colunas
theme_bw()+
theme(legend.title = element_text(size=12, color="Black", family="Merriweather", face="bold.italic"),# Formatação dos textos e eixos
plot.title = element_text(family = "Playfair", color="black", size=20, face="bold.italic"),
plot.subtitle = element_text(family = "Merriweather", color="gray24", size=12, face="bold.italic"),
axis.title = element_text(family = "Lato", color="black", size = 12, face="bold"),
axis.text = element_text(family = "Lato", color="black", size = 10, face="bold"))+
scale_fill_brewer(palette = "Set1")+ # Cor das barras
labs(title = "Histogram", # TÃtulo, subtÃtulo, legenda e eixos
subtitle = "Flipper Length by Species",
x = "Flipper Length (mm)",
y = "Absolute Frequence (N)",
fill = "Species"))Density Plot
Basic
penguins %>%
ggplot(aes(flipper_length_mm))+
geom_density()Complete
(penguins %>% drop_na(flipper_length_mm) %>%
ggplot(aes(flipper_length_mm)) +
geom_density(alpha=1, fill = "firebrick")+ #cor e densidade do gráfico
geom_vline(aes(xintercept=mean(flipper_length_mm, na.rm=TRUE)), color="black", linetype="dashed", size=1)+ #linha da média
theme_bw()+
theme(legend.position = "none", # Formatação dos textos e eixos
plot.title = element_text(family = "Playfair", color="black", size=20, face="bold.italic"),
plot.subtitle = element_text(family = "Merriweather", color="gray24", size=12, face="bold.italic"),
axis.title = element_text(family = "Lato", color="black", size = 12, face="bold"),
axis.text = element_text(family = "Lato", color="black", size = 10, face="bold"))+
labs(title = "Density Plot", # TÃtulo, subtÃtulo e tÃtulos dos eixos
subtitle = "Flipper Length",
x = "Flipper Length (mm)",
y = 'Density'))Separated by groups
mean <- ddply(penguins, "species", summarise, grp.mean=mean(flipper_length_mm, na.rm=TRUE))
(penguins %>% drop_na(flipper_length_mm) %>%
ggplot(aes(flipper_length_mm, group=species, fill=species)) +
geom_density(alpha=0.6)+ #densidade do gráfico
geom_vline(data=mean, aes(xintercept=grp.mean, color = species),linetype="dashed", size=1, show.legend = FALSE)+ #linha da média
theme_bw()+
theme(legend.title = element_text(size=12, color="Black", family="Merriweather", face="bold.italic"), # Formatação dos textos e eixos
plot.title = element_text(family = "Playfair", color="black", size=20, face="bold.italic"),
plot.subtitle = element_text(family = "Merriweather", color="gray24", size=12, face="bold.italic"),
axis.title = element_text(family = "Lato", color="black", size = 12, face="bold"),
axis.text = element_text(family = "Lato", color="black", size = 10, face="bold"))+
scale_fill_brewer(palette = "Set1")+ # Cor das curvas
scale_color_brewer(palette = "Set1")+
labs(title = "Density Plot", # TÃtulo, subtÃtulo e tÃtulos dos eixos
subtitle = "Flipper Length by Species",
x = "Flipper Length (mm)",
y = "Density",
fill = "Species"))Histogram with Density Plot
mean <- ddply(penguins, "species", summarise, grp.mean=mean(flipper_length_mm, na.rm=TRUE))
(penguins %>% drop_na(flipper_length_mm) %>%
ggplot(aes(flipper_length_mm, group=species, fill=species)) +
geom_histogram(aes(y=..density..), alpha=0.5, position = "identity", color="black", bins = 20) + # alpha muda densidade da cor, bins é a quantidade de colunas
geom_density(alpha=0.3)+ #cor e densidade do gráfico
geom_vline(data=mean, aes(xintercept=grp.mean, color = species),linetype="dashed", size=1, show.legend = FALSE)+ #linha da média
theme_bw()+
theme(legend.title = element_text(size=12, color="Black", family="Merriweather", face="bold.italic"), # Formatação dos textos e eixos
plot.title = element_text(family = "Playfair", color="black", size=20, face="bold.italic"),
plot.subtitle = element_text(family = "Merriweather", color="gray24", size=12, face="bold.italic"),
axis.title = element_text(family = "Lato", color="black", size = 12, face="bold"),
axis.text = element_text(family = "Lato", color="black", size = 10, face="bold"))+
scale_fill_brewer(palette = "Set1")+ # Cor das curvas
scale_color_brewer(palette = "Set1")+
labs(title = "Histogram with Density Plot", # TÃtulo, subtÃtulo e tÃtulos dos eixos
subtitle = "Flipper Length by Species",
x = "Flipper Length (mm)",
y = "Density",
fill = "Species"))Pie Chart
Basic
# o ggplot não tem uma função especÃfica direito para fazer piechart, então é um pouco remendado
# Não consegui adicionar de uma forma boa os números no gráfico
(penguins %>%
group_by(species) %>% # separação nos grupos de espécies
dplyr::summarise(N = n()) %>% # N = contagem das espécies
ggplot(aes(x = "", y = N, fill = species)) +
geom_col(width = 1) + # utilizamos um bar graph que vai ser arredondado para virar o pie chart
coord_polar(theta = "y", start = 0)) # função utilizada para montar pie chartComplete
(penguins %>%
dplyr::mutate(n_all = n()) %>% # n_all = soma de todas contagens únicas
group_by(species) %>% # separação nos grupos de espécies
dplyr::summarise(N = n() / unique(n_all)) %>% # N = contagem das espécies / total de contagens únicas
ggplot(aes(x = "", y = N, fill = species)) +
geom_col(width = 1) +
coord_polar(theta = "y", start = 0)+ # função utilizada para montar pie chart
labs(title = "Pie Chart", # TÃtulo, subtÃtulo, legenda e eixos
subtitle = "Species",
x = "",
y = "Proportion of each species",
fill = "Species")+
scale_fill_brewer(palette = "Set1")+ # cor de cada grupo
theme(legend.title = element_text(size=12, color="Black", family="Merriweather", face="bold.italic"),# Formatação dos textos e eixos
plot.title = element_text(family = "Playfair", color="black", size=20, face="bold.italic"),
plot.subtitle = element_text(family = "Merriweather", color="gray24", size=12, face="bold.italic"),
axis.title = element_text(family = "Lato", color="black", size = 12, face="bold"),
axis.text = element_text(family = "Lato", color="black", size = 10, face="bold"),
axis.ticks = element_blank(),
panel.grid = element_blank()))Boxplot
Basic
penguins %>% drop_na(flipper_length_mm) %>%
ggplot(aes(x = species, y = flipper_length_mm))+
geom_boxplot()Complete
(penguins %>% drop_na(flipper_length_mm) %>%
ggplot(aes(x = species, y = flipper_length_mm)) +
geom_boxplot(aes(color = species), width = 0.5, show.legend = FALSE) + # estética dos box plots
geom_jitter(aes(color = species), alpha = 0.7, show.legend = FALSE, position = position_jitter(width = 0.2, seed = 0)) + # estética das bolinhas (jitter)
theme_bw()+
theme(legend.title = element_text(size=12, color="Black", family="Merriweather", face="bold.italic"), # Formatação dos textos e eixos
plot.title = element_text(family = "Playfair", color="black", size=20, face="bold.italic"),
plot.subtitle = element_text(family = "Merriweather", color="gray24", size=12, face="bold.italic"),
axis.title = element_text(family = "Lato", color="black", size = 12, face="bold"),
axis.text = element_text(family = "Lato", color="black", size = 10, face="bold"))+
scale_color_brewer(palette = "Set1")+ # Cor dos box plots
labs(title = "Box Plot", # TÃtulo, subtÃtulo e tÃtulos dos eixos
subtitle = "Flipper Length by Species",
x = "Species",
y = "Flipper Length (mm)",
fill = "Species"))Raincloud Plot
penguins %>% drop_na(flipper_length_mm) %>%
ggplot(aes(x=species, y=flipper_length_mm))+
ggdist::stat_halfeye(aes(fill = species), alpha = 0.4, show.legend = FALSE, adjust = .5, width = .6, .width = 0, justification = -.3) +
geom_boxplot(aes(color = species), width = 0.25,show.legend = FALSE, outlier.shape = NA) + # estética dos box plots
geom_jitter(aes(color = species), alpha = 0.4, position = position_jitter(width = 0.1, seed = 0)) +
#geom_point(size = 1.3, alpha = .3, position = position_jitter(seed = 1, width = .1))+
theme_bw()+
theme(legend.title = element_text(size=12, color="Black", family="Merriweather", face="bold.italic"), # Formatação dos textos e eixos
plot.title = element_text(family = "Playfair", color="black", size=20, face="bold.italic"),
plot.subtitle = element_text(family = "Merriweather", color="gray24", size=12, face="bold.italic"),
axis.title = element_text(family = "Lato", color="black", size = 12, face="bold"),
axis.text = element_text(family = "Lato", color="black", size = 10, face="bold"))+
scale_color_brewer(palette = "Set1")+ # Cor dos box plots
scale_fill_brewer(palette = "Set1")+ # Cor dos box plots
labs(title = "Raincloud Plot", # TÃtulo, subtÃtulo e tÃtulos dos eixos
subtitle = "Flipper Length by Species",
x = "Species",
y = "Flipper Length (mm)",
color = "Species")+
guides(color = guide_legend(override.aes = list(size = 5)))+
coord_cartesian(xlim = c(1.2, NA), clip = "off")Scatter Plot
Basic
(penguins %>% drop_na(flipper_length_mm) %>%
ggplot(aes(flipper_length_mm, bill_length_mm))+
geom_point())bill_len_dep <- ggplot(data = penguins,
aes(x = bill_length_mm,
y = bill_depth_mm,
group = species)) +
geom_point(aes(color = species,
shape = species),
size = 3,
alpha = 0.8) +
geom_smooth(method = "lm", se = FALSE, aes(color = species)) +
scale_color_manual(values = c("darkorange","purple","cyan4")) +
labs(title = "Penguin bill dimensions",
subtitle = "Bill length and depth for Adelie, Chinstrap and Gentoo Penguins at Palmer Station LTER",
x = "Bill length (mm)",
y = "Bill depth (mm)",
color = "Penguin species",
shape = "Penguin species") +
theme(legend.position = c(0.85, 0.15),
plot.title.position = "plot",
plot.caption = element_text(hjust = 0, face= "italic"),
plot.caption.position = "plot")Complete
(penguins %>% drop_na(flipper_length_mm, bill_length_mm) %>%
ggplot(aes(flipper_length_mm, bill_length_mm))+
geom_point(color="firebrick", size = 2, alpha = 1) +
geom_smooth(method = "lm", se = FALSE, color="dodgerblue4") +
theme_bw()+
theme(legend.position = "none", # Formatação dos textos e eixos
plot.title = element_text(family = "Playfair", color="black", size=20, face="bold.italic"),
plot.subtitle = element_text(family = "Merriweather", color="gray24", size=12, face="bold.italic"),
axis.title = element_text(family = "Lato", color="black", size = 12, face="bold"),
axis.text = element_text(family = "Lato", color="black", size = 10, face="bold"))+
labs(title = "Scatter Plot", # TÃtulo, subtÃtulo e tÃtulos dos eixos
subtitle = "Flipper Length x Bill Length",
x = "Flipper Length (mm)",
y = "Bill Length (mm)"))Separated by Species
(penguins %>% drop_na(flipper_length_mm, bill_length_mm) %>%
ggplot(aes(flipper_length_mm, bill_length_mm))+
geom_point(aes(color=species, shape=species), size = 2, alpha = 1) +
geom_smooth(method = "lm", se = FALSE, aes(color = species)) +
theme_bw()+
theme(legend.title = element_text(size=12, color="Black", family="Merriweather", face="bold.italic"), # Formatação dos textos e eixos
plot.title = element_text(family = "Playfair", color="black", size=20, face="bold.italic"),
plot.subtitle = element_text(family = "Merriweather", color="gray24", size=12, face="bold.italic"),
axis.title = element_text(family = "Lato", color="black", size = 12, face="bold"),
axis.text = element_text(family = "Lato", color="black", size = 10, face="bold"))+
scale_color_brewer(palette = "Set1")+ # Cor dos box plots
scale_fill_brewer(palette = "Set1")+ # Cor dos box plots
labs(title = "Scatter Plot", # TÃtulo, subtÃtulo e tÃtulos dos eixos
subtitle = "Flipper Length x Bill Length by Species",
x = "Flipper Length (mm)",
y = "Bill Length (mm)",
color = "Species",
shape = "Species"))